package modulesAPI.v2.composite;

import modulesAPI.v2.Module;
import modulesAPI.v2.ViewType;
import warehouse.Resource;
import warehouse.Table;
import warehouse.Gazebo;

import com.caterina.components.CPanel;
import com.caterina.resources.CModules;
import com.caterina.resources.CWords;

public class MResource extends Module<Resource>{
	/**
	 * Crea un nuovo modulo per l'inserimento/rappresentazione di una risorsa
	 * */
	public MResource(Module<?> ownerModule, Resource content, String tag,String description, ViewType viewType,int...options) {
		super(ownerModule, content, tag, description, viewType,options);
		if(content instanceof Table){
			table = new MTable(this, (Table)content, "TABLE", "TABLE_DESC", viewType);
			gazebo = null;
		}else if(content instanceof Gazebo){
			gazebo = new MGazebo(this, (Gazebo)content, "GAZEBO", "GAZEBO_DESC", viewType);
			table = null;
		}
	}

	@Override
	protected CPanel getReadOnlyPanel(boolean isSelected) {
		if(table != null)
			return table.getDefaultPanel();
		return gazebo.getDefaultPanel();
	}
	@Override
	protected CPanel getWritablePanel(boolean isSelected) {
		if(table != null)
			return table.getDefaultPanel();
		return gazebo.getDefaultPanel();
	}
	@Override
	public CPanel getInListPanel(boolean isSelected) {
		return CModules.formatForList(Long.toString(this.getContent().getId()), this.getContent().getDisponibility()?CWords.getWord("DISPONIBLE"):CWords.getWord("NOT_DISPONIBLE"), isSelected);
	}
	@Override
	protected CPanel getOneLinePanel(boolean isSelected) {
		return null;
	}
	protected void beforeFireChange(){
		if(this.getContent() instanceof Table)
			this.setContent(table.getContent());
		else if(this.getContent() instanceof Gazebo)
			this.setContent(gazebo.getContent());
	}
	@Override
	protected void beforeFireChange(Module<?> mod) {
		
	}

	@Override
	protected void afterFireChange(Module<?> mod) {
		
	}
	private MTable table;
	private MGazebo gazebo;
}
